package com.gw.quotes.activity;

import java.util.HashMap;

import net.tsz.afinal.annotation.view.ViewInject;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.gw.quotes.R;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.GetMapInfoImpl;
import com.gw.quotes.util.CommonTools;
import com.yxw.util.StringUtils;

public class ChangePassActivity extends BaseActivity {

	@ViewInject(id = R.id.back_img, click = "back")
	private ImageView back_img;
	@ViewInject(id = R.id.bt_submit, click = "onSubmit")
	private Button bt_submit;
	@ViewInject(id = R.id.et_new_pass)
	private EditText et_new_pass;
	@ViewInject(id = R.id.et_new_pass2)
	private EditText et_new_pass2;
	@ViewInject(id = R.id.et_old_pass)
	private EditText et_old_pass;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_changepassword);
	}

	public void back(View v) {
		finish();
	}

	public void onSubmit(View v) {
		String oldString = et_old_pass.getText() + "";
		String newString = et_new_pass.getText() + "";
		String new2String = et_new_pass2.getText() + "";
		if (StringUtils.isEmpty(oldString)) {
			CommonTools.showShortToast(ChangePassActivity.this, "原密码不能为空！");
			return;
		}
		if (StringUtils.isEmpty(newString)) {
			CommonTools.showShortToast(ChangePassActivity.this, "新密码不能为空！");
			return;
		}
		if (StringUtils.isEmpty(new2String)) {
			CommonTools.showShortToast(ChangePassActivity.this, "请确认密码！");
			return;
		}
		if (!new2String.equals(newString)) {
			CommonTools.showShortToast(ChangePassActivity.this, "新密码不一致！");
			return;
		}
		dialog.show();
		BaseServiceInter httpService = new GetMapInfoImpl("changePassword");
		Request request = new Request();
		request.setParas(getLoginUser().getLoginname(), getLoginUser()
				.getTicket(), "{'oldpassword':'" + et_old_pass.getText()
				+ "','newpassword':'" + et_new_pass.getText() + "'}");
		Response response = httpService.handler(request,
				new HttpCallBack<Response<HashMap<String, Object>>>() {
					@Override
					public void onFinished(
							Response<HashMap<String, Object>> response) {
						if (response.getResult() == Response.RESULT_SUCCESS) {
							// Toast.makeText(LoginActivity.this,response.getMsg(),Toast.LENGTH_SHORT).show();
							getLoginUser().setPassword(
									et_new_pass.getText().toString());
							aCache.put("logininfo", getLoginUser()
									.getLoginname()
									+ "||"
									+ getLoginUser().getPassword());
							CommonTools.showShortToast(ChangePassActivity.this,
									"密码修改成功！");
							finish();
						} else {
							CommonTools.showShortToast(ChangePassActivity.this,
									response.getMsg());
							// Toast.makeText(LoginActivity.this,response.getMsg(),Toast.LENGTH_SHORT).show();
						}
						dialog.dismiss();
					}
				});
	}
}
